<?php
// -----------------------------------------------------------------------------
// include
// -----------------------------------------------------------------------------

include "../config/config.php";
include "../auth.php";
include "../DBManager.php";

// -----------------------------------------------------------------------------
// 访问设置
// -----------------------------------------------------------------------------

if (!isset($_POST["id"])) {
    exit("需要传入有效的参数！");
}

// -----------------------------------------------------------------------------
// 时区设置
// -----------------------------------------------------------------------------

date_default_timezone_set($region);
$lastEditTime = (string)time();

// -----------------------------------------------------------------------------
// 数据库相关设置
// -----------------------------------------------------------------------------

$dbPath = "../" . $dbPath; // 不加这行语句的话会报错，提示找不到对应路径
$db = new DBManager("sqlite:" . $dbPath);

// -----------------------------------------------------------------------------

$goalInfor = [];

$goalInfor["id"]           = $_POST["id"];
$goalInfor["description"]  = $_POST["description"]; // 目标描述
$goalInfor["startDate"]    = $_POST["startDate"]; // 原定开始日期
$goalInfor["endDate"]      = $_POST["endDate"]; // 原定结束日期
$goalInfor["motivation"]   = $_POST["motivation"]; // 行动动力
$goalInfor["actionPlan"]   = $_POST["actionPlan"]; // 行动计划
$goalInfor["remark"]       = $_POST["remark"]; // 备注信息

$goalInfor["status"]       = $_POST["status"]; // 完成状态
$goalInfor["achieveDate"]  = $_POST["achieveDate"]; // 目标实现日期
$goalInfor["giveupDate"]   = $_POST["giveupDate"]; // 目标放弃日期
$goalInfor["giveupReason"] = $_POST["giveupReason"]; // 目标放弃原因

$goalInfor["lastEditTime"] = $lastEditTime; // 最后编辑日期

// -----------------------------------------------------------------------------

if (!$db->goalExist($goalInfor["id"])) {
    exit("指定的目标不存在！");
}

try {
    $db->updateIntoGoals($goalInfor);
    echo "数据保存成功！";
} catch (Exception $e) {
    echo "出错啦，错误描述如下：";
    echo "<br />";
    echo $e->getMessage();
}
